package org.wk.upms.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wk.core.common.exception.ExceptionCode;
import org.wk.core.web.exception.GeneralException;
import org.wk.upms.common.annotation.DataFilter;
import org.wk.upms.common.constant.Constant;
import org.wk.upms.dao.DeptDao;
import org.wk.upms.domain.DeptEntity;
import org.wk.upms.domain.DeptEntity;
import org.wk.upms.view.DeptVo;
import org.wk.upms.view.DeptVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
    
    @Autowired
    private DeptDao deptDao;
    
    @Override
    @DataFilter(subDept = true, user = false)
    public List<DeptVo> queryList(HashMap<String, Object> params) {
        List<DeptEntity> deptList = deptDao.findAll();
        List<DeptVo> deptVos = new ArrayList<>();
        if (deptList!=null && !deptList.isEmpty()) {
            DeptVo deptVo;
            for (DeptEntity deptEntity : deptList) {
                deptVo = new DeptVo();
                BeanUtils.copyProperties(deptEntity, deptVo);
                deptVo.setDeptId(deptEntity.getId());
                deptVos.add(deptVo);
            }
        }
        return deptVos;
    }

    @Override
    public DeptVo selectById(String deptId) {

        DeptEntity deptEntity = deptDao.getOne(deptId);

        DeptVo deptVo = new DeptVo();
        BeanUtils.copyProperties(deptEntity, deptVo);
        deptVo.setDeptId(deptEntity.getId());
        return deptVo;
    }

    @Override
    public void insert(DeptVo dept) {
        DeptEntity depts = deptDao.findByName(dept.getName());
        if (null != depts){
            throw new GeneralException(ExceptionCode.ex_dept_001);
        }
        DeptEntity deptEntity = new DeptEntity();
        BeanUtils.copyProperties(dept,deptEntity);
        deptDao.save(deptEntity);
    }

    @Override
    public void updateById(DeptVo dept) {
        String deptId = dept.getDeptId();
        DeptEntity deptEntity = deptDao.getOne(deptId);
        BeanUtils.copyProperties(dept, deptEntity);
        deptDao.update(deptEntity);
    }

    @Override
    public List<String> queryDeptIdList(String deptId) {

        return null;
    }

    @Override
    public void deleteById(String deptId) {
        deptDao.deleteById(deptId);
    }
}
